#include "terminalHandler.h"


namespace terminal {
	terminalHandler::terminalHandler() {
		level = 0;
		prefix();
		pre = false;

		NothingToBeDone = "NothingToBeDone";
		readArticle = "readArticle";
		WriteNewsgroups = "WriteNewsgroups";
		WriteArticles = "WriteArticles";
		deleteNewsgroup = "deleteNewsgroup";
		deleteArticle = "deleteArticle";
		createNewsgroup = "createNewsgroup";
		createArticle = "createArticle";
		//		
		
		manual = "Error : No such input. There are four directories/levels that you can stand in: 0-3. In these four levels there are different commands that are valid. You can always type 'cd ..' to get to a lower level, 'level' to get an outprint of what level you are standing in and 'exit' to end the program. //// You start in level 0. In level 0 valid commands are 'exit', 'list' and level. 'List' shows a list of all newsgroups with the id first and the name last. This command also makes you stand in level 1. //// In level 1 the valid commands are 'exit','cd ..', 'list someIdNumber', 'delete someIdNumber', 'create newsgroup' and 'level'. 'cd ..' makes you stand in level 0. 'list someIdNumber' lists all the articles in that newsgroup with the id first and the name last. someIdNumber is a id-number of a newsgroup.This command also makes you stand in level 2. 'delete someIdNumber' delete that newsgroup. 'create newsgroup' creates a new newsgroup with a given name. //// In level 2 the valid commands are 'exit', 'cd ..', 'read someIdNumber', 'delete someIdNumber', 'create article' and 'level'. 'cd ..' makes you stand in level 1. 'read someIdNumber' shows the  article. someIdNumber is a id-number of a article. This command also makes you stand in level 3. 'delete someIdNumber' delete that article. 'create article' creates an article with a given name. //// In level 3 the only valid commands are 'exit', 'cd ..' and level. 'cd' makes you stand in level 2.";
	}

	terminalHandler::~terminalHandler() {
	}

	void terminalHandler::add(int n) {
		level += n;
	}


	container terminalHandler::read() {
		if(pre == true) {
			prefix();
			pre = false;
		}		
		message.clear();
		getline(cin, message);

		if(message == "level") {
			cout << "You are standing in level " << level << endl;
			prefix();
			container newtuple("NothingToBeDone",newsGroupId,articleId,author,name,text,0);
			return newtuple;
		}
		

		if(message == "exit") {
			container newtuple("exit",newsGroupId,articleId,author,name,text,0);
			return newtuple;	
		}


		
		if(level == 0) {
			return level0();
		}

		if(level == 1) {
			return level1();
		}

		if(level == 2) {
			return level2();
		}

			
		return level3();
	}
	

	
	void terminalHandler::write(int n, stringstream& os) {
		cout << n << " ";
		string out;
		while(os >> out) {
			cout << out << " ";
			out.clear();
		}
		cout << endl;
	}

//---------------------------------------------------------------------------------------------------------------//
	container terminalHandler::level0() {
		if(message == "list") {
			pre = true;
			container newtuple(WriteNewsgroups,newsGroupId,articleId,author,name,text,1);
			return newtuple;
		}
		write(manual);
		container newtuple(NothingToBeDone,newsGroupId,articleId,author,name,text,0);
		return newtuple;
	}

//---------------------------------------------------------------------------------------------------------------//
	container terminalHandler::level1() {
		if(message == "cd ..") {
			prefix();
			container newtuple(NothingToBeDone,newsGroupId,articleId,author,name,text,-1);
			return newtuple;
		}
		if(message == "create newsgroup") {
			pre = true;
			cout << "Type a name for the newsgroup: ";
			text.clear();
			getline(cin, name);
			container newtuple(createNewsgroup,newsGroupId,articleId,author,name,text,0);
			return newtuple;
		}
		
		string str;
		stringstream ss(message);

		if(ss >> str) {
			
			if(str == "list") {
				int number;
				if(ss >> number) {
					pre = true;
					newsGroupId = number;
					container newtuple(WriteArticles,newsGroupId,articleId,author,name,text,1);
					return newtuple;
				}
			}
			if(str == "delete") {
				int number;
				if(ss >> number) {
					pre = true;
					newsGroupId = number;
					container newtuple(deleteNewsgroup,newsGroupId,articleId,author,name,text,0);
					return newtuple;
				}
			}
		
		}
		write(manual);
		container newtuple(NothingToBeDone,newsGroupId,articleId,author,name,text,0);
		return newtuple;
	}	
//---------------------------------------------------------------------------------------------------------------//
	container terminalHandler::level2() {
		if(message == "cd ..") {
			pre = true;
			container newtuple(WriteNewsgroups,newsGroupId,articleId,author,name,text,-1);
			return newtuple;
		}

		if(message == "create article") {
			pre = true;
			text.clear();
			
			cout << "Type the name of the author of the article: ";
			getline(cin, author);
			cout << "Type the name of the article: ";
			getline(cin, name);
			cout << "Write the article: ";
			getline(cin,text);
			container newtuple(createArticle,newsGroupId,articleId,author,name,text,0);
			return newtuple;
		}

		string str;
		stringstream ss(message);
		if(ss >> str) {
			if(str == "read") {
				int number;
				if(ss >> number) {
					pre = true;
					articleId = number;
					container newtuple(readArticle,newsGroupId,articleId,author,name,text,1);
					return newtuple;
				}
			}
			
			if(str == "delete") {
				int number;
				if(ss >> number) {
					pre = true;
					articleId = number;
					container newtuple(deleteArticle,newsGroupId,articleId,author,name,text,0);
					return newtuple;
				}
			}
		}
		write(manual);
		container newtuple(NothingToBeDone,newsGroupId,articleId,author,name,text,0);
		return newtuple;
	}


//---------------------------------------------------------------------------------------------------------------//
	container terminalHandler::level3() {
		if(message == "cd ..") {
			pre = true;
			container newtuple(WriteArticles,newsGroupId,articleId,author,name,text,-1);
			return newtuple;
		}

		write(manual);
		container newtuple(NothingToBeDone,newsGroupId,articleId,author,name,text,0);
		return newtuple;
	}
//---------------------------------------------------------------------------------------------------------------//

	
	void terminalHandler::write(string s){
		cout << s << endl;
		prefix();
	}
	
	void terminalHandler::prefix() {
		cout << "news> ";
	}

}

